﻿using NFine.Data;
using NFine.Data.Extensions;
using NFine.Domain._03_Entity.Weixin;
using NFine.Domain._04_IRepository.Weixin;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NFine.Repository.Weixin
{
    public class UserInfoRepository : RepositoryBase<UserInfoEntity>, IUserInfoRepository
    {
        //public new int Insert(UserInfoEntity entity)
        //{
        //    entity.USERNUMBER = "00000000";
        //    String sql = "INSERT INTO user_info(USERNICKNAME,USERHEAD,USERSEX,WECHATACCOUNT,USERNUMBER) VALUES('{0}','{1}',{2},'{3}','{4}')";
        //    return DbHelper.ExecuteSqlCommand(String.Format(sql,entity.USERNICKNAME,entity.USERHEAD,entity.USERSEX,entity.WECHATACCOUNT,entity.USERNUMBER));
        //}
        public int InsertUserDynamic(UserInfoEntity entity)
        {
            return DbHelper.ExecuteSqlCommand("INSERT INTO User_dynamic (userid) VALUES(" + entity.USERID + ")");
        }

        public int InsertUserOpenID(int usernumber, string uid, string oid)
        {
            return DbHelper.ExecuteSqlCommand("INSERT INTO wx_usernumber_openid(unionid,openid,usernumber) VALUES('" + uid + "','" + oid + "'," + usernumber + ")");
        }

        /// <summary>
        /// 检查用户的OpenID是否已经存在，如果不存则插入一个
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="oid"></param>
        public void checkuseropenid(UserInfoEntity entity, string oid)
        {
            int unum = 0;
            if (Int32.TryParse(entity.USERNUMBER, out unum))
            {
                if (!((new RepositoryBase<UserNumberOpenIDEntity>()).IQueryable().Any(o => o.usernumber == unum)))
                {
                    InsertUserOpenID(unum, entity.WECHATACCOUNT, oid);
                }
            }

        }

    }
}
